package com.kgc.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 生成一个单例SqlSessionFactory
 */
public class MyBatisFactoryUtils {
    private static SqlSessionFactory factory;
    private MyBatisFactoryUtils(){}
    static {
        try{
            InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
            factory=new SqlSessionFactoryBuilder().build(in);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    public static SqlSession openSession(){
        //设置sqlSession自动提交  sqlSession.commit();
        return factory.openSession(true);
    }
}
